package base.exa18_1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("麻子.txt"));
        // 读取指定文件的内容
        String[] ps = readInfo(scanner);
        // 信息提取
        PersonInfo personInfo = getPersonInfo(ps);
        System.out.println("简历的基本信息为："+personInfo.toString());
    }


    /**
     * 信息提取
     * @param params
     * @return
     */
    public static PersonInfo getPersonInfo(String[] params){
        PersonInfo personInfo = new PersonInfo();
        for(String s:params){
            if(s!=null){
                if(s.contains("姓名")){
                    s = s.replace("姓名","");
                    s = s.replace("\r","");
                    personInfo.setName(s);
                }
                if(s.contains("年龄")){
                    String age = "";
                    for(int i=0;i<s.length();i++) {
                        if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
                            age += s.charAt(i);
                        }
                    }
                    personInfo.setAge(Integer.valueOf(age));
                }
                if(s.contains("毕业院校")){
                    s = s.replace("毕业院校","");
                    s = s.replace("\r","");
                    personInfo.setSchool(s);
                }
                if(s.contains("专业")){
                    s = s.replace("专业","");
                    s = s.replace("\r","");
                    personInfo.setProfessional(s);
                }
            }
        }
        return personInfo;
    }



    /**
     * 信息扫描
     * @param scanner
     * @return
     */
    public static String[] readInfo(Scanner scanner){
        String [] params = new String[2];
        scanner.useDelimiter("\n");
        int i = 0;
        while (true){
            if(scanner.hasNext()){
                if(i==params.length){
                    params = dilatation(params);
                }
                params[i++] = scanner.next();
            }else break;
        }
        return params;
    }


    /**
     * 字符串数组扩容
     * @param source
     * @return
     */
   public static String[] dilatation(String[] source){
        String[] result = new String[2*source.length+5];
        for (int i=0;i<source.length;i++){
            result[i] = source[i];
        }
        return result;
    }

}
